#pragma once

#include "ui.h"
#include "ui_base.h"

_SSL_BEGIN
_SSUIL_BEGIN

class SSUI_CLASS RawWindow :
	public supports_weak_ptr<RawWindow>,
	virtual public ref_counted_base
{
public:
	RawWindow(const tchar *class_name = NULL);
	virtual ~RawWindow();

public:
	void SetWindowClassName(const tchar *class_name);
	void SetInstance(HINSTANCE inst);
	void SetTransparentColor(unsigned long transparent_color);

public:
	bool Destroy();
	HWND GetWindow(HWND parent_h = NULL, UINT id = 0);
	bool IsValid() const;
	void BringToFront();

protected:
	HINSTANCE GetInstance();
	tstring GetClassNameWithTransparentColor();

public:
	static bool RegisterClassIfNecessary(HINSTANCE inst, const tchar *class_name, unsigned long transparent_color);

protected:
	static LRESULT CALLBACK ___WindowProc___(HWND window_h, UINT msg, WPARAM wparam, LPARAM lparam);

protected:
	HINSTANCE inst_;
	HWND window_h_;
	unsigned long transparent_color_;
	tchar class_name_[MAX_CLASS_NAME];
};

#pragma warning(push)
#pragma warning(disable:4251)
#pragma warning(disable:4250)
#pragma warning(disable:4275)
class SSUI_CLASS UIWindow :
	public IUIWindow,
	virtual public ref_counted_base
{
public:
	UIWindow();
	virtual ~UIWindow();

	// ui_window_interface
public:
	virtual scoped_ref_ptr<IUIManagerInterface> GetWindowManager();
	virtual bool SetWindowPresetLeftTopPoint(const UIPoint &pt);
	virtual bool SetWindowPresetSize(const UISize &st);
	virtual bool ShowWindow(bool show);
	virtual bool ShowWindowEx(bool show, bool center, bool init, bool show_noactive);
	virtual bool IsWindowVisibled();
	virtual bool IsValid() const;
	virtual bool SetReleaseOnDestoryMessage(bool release_ref);
	virtual bool IsReleaseOnDestoryMessage() const;
	virtual bool Destroy();

public:
	virtual bool Init(HWND parent_win, UINT id, HINSTANCE inst, unsigned long transparent_color,
		IUIWindowBgCanvasInterface *bg_canvas,
		IUIResourceRenderInterface *res_ren, IUIDiskFilesManagerInterface *disk_man,
		IUIBuilderInterface *builder);
	virtual void Uninit();

	virtual bool ApplyAttributes(IUIBuilderInterface *ui_builder, 
		IUIXMLDocumentInterface *xml_document, UIXmlNode node);

	virtual void SetName(const tchar *name);
	virtual const tchar* GetName() const;
	virtual void SetMainWindow(bool is_main);
	virtual bool IsMainWindow() const;
	virtual RawWindow* GetRawWindow();
	virtual void SetTooltip(UIWindow *tooltip);

public:
	LRESULT WindowProc(UINT msg, WPARAM wparam, LPARAM lparam, bool &handled);

protected:
	virtual LRESULT OnWindowProc(UINT msg, WPARAM wparam, LPARAM lparam, bool &handled);

protected:
	static LRESULT CALLBACK ___HookedWindowProc___(HWND window_h, UINT msg, WPARAM wparam, LPARAM lparam);

protected:
	tstring name_;
	bool is_main_window_;
	RawWindow raw_window_;
	WNDPROC wnd_proc_;
	scoped_ref_ptr<IUIManagerInterface> win_man_;
	UIPoint win_pt_;
	UISize win_sz_;
	scoped_ref_ptr<UIWindow> tooltip_;
	bool release_on_destory_message_;
};

class SSUI_CLASS UIDialog :
	public UIWindow,
	public IUINotifyReceiverInterface
{
public:
	UIDialog(const tstring &prefix_end_dlg_button_name = _T(""));
	virtual ~UIDialog();

public:
	virtual bool Init(HWND parent_win, UINT id, HINSTANCE inst, unsigned long transparent_color,
		IUIWindowBgCanvasInterface *bg_canvas,
		IUIResourceRenderInterface *res_ren, IUIDiskFilesManagerInterface *disk_man,
		IUIBuilderInterface *builder);
	virtual void Uninit();
	virtual RawWindow* GetRawWindow();
	virtual void SetWindowSize(UISize &sz);
	virtual bool ShowWindow(int show_method);

public:
	virtual void OnNotify(UINotify &notify);

public:
	virtual tstring DoModal();
	virtual tstring GetResult();

protected:
	virtual bool ReallyInit(HWND window_h);

protected:
	static INT_PTR CALLBACK ___HookedDialogProc___(HWND window_h, UINT msg, WPARAM wparam, LPARAM lparam);

protected:
	HWND parent_window_h_;
	HINSTANCE inst_;
	scoped_ref_ptr<IUIWindowBgCanvasInterface> bg_canvas_;
	scoped_ref_ptr<IUIResourceRenderInterface> render_manager_;
	scoped_ref_ptr<IUIDiskFilesManagerInterface> disk_files_manager_;
	scoped_ref_ptr<IUIBuilderInterface> builder_;
	HWND window_h_;
	tstring result_;
	tstring prefix_end_dlg_button_name_;
};

class SSUI_CLASS ui_menu_window : public UIWindow
{
	// ui_window
public:
	virtual bool Init(HWND parent_win, UINT id, HINSTANCE inst, unsigned long transparent_color,
		IUIWindowBgCanvasInterface *bg_canvas,
		IUIResourceRenderInterface *res_ren, IUIDiskFilesManagerInterface *disk_man,
		IUIBuilderInterface *builder);

protected:
	virtual LRESULT OnWindowProc(UINT msg, WPARAM wparam, LPARAM lparam, bool &handled);
};

_SSUIL_END
_SSL_END
